package com.easyban.utils;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class ValidateCodeUtils {
    public static Map<String, String> createCaptcha() throws IOException {
        //设置验证码宽高，字数，干扰线条
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(100, 50, 4, 100);
        //设置背景颜色
        lineCaptcha.setBackground(Color.decode("#f9f4dc"));
        String code = lineCaptcha.getCode();
        BufferedImage image = lineCaptcha.getImage();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "png", baos);
        byte[] imageBytes = baos.toByteArray();
        String s = Base64.getEncoder().encodeToString(imageBytes);

        Map<String, String> captchaMap = new HashMap<>();
        captchaMap.put(code, s);
        return captchaMap;
    }
}
